package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Connect;
import model.SQLModel;
import model.Sales;
import model.Table;
import model.UserInfo;
import model.table.DMLTableInterface;
import model.table.ItemTable;
import model.table.SaleReportTable;

/**
 * Servlet implementation class ServletLogin
 */
public class ViewSaleReportTable extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewSaleReportTable() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {

			if (request.getMethod().equalsIgnoreCase("post")
					|| request.getMethod().equalsIgnoreCase("get")) {
				try {
					Connect connect = new Connect();
					Account account = connect.checkSession(request, response);
					if (!"admin_role".equalsIgnoreCase(account.strRole)) {
						connect.setSID(connect.getdbSession(request));
						connect.setUser(account.strUserName);
						connect.setPassword(account.strPass);
						Connection conn = connect.simple_connect();
						String owner = (String) request.getParameter("owner");

						DMLTableInterface db = new SaleReportTable(conn, owner);
						// selectsc
						LinkedList<Sales> lst = db.select();
						SQLModel db1 = new SQLModel(conn);
						LinkedList<Table> lst1 = db1.getAccessTablesOfUser(account.strUserName);
						conn.close();
						RequestDispatcher dispatcher = getServletContext()
								.getRequestDispatcher(
										"/JSP/userView/salerTables.jsp");
						request.setAttribute("lstSale", lst);
						request.setAttribute("lstTable", lst1);
						request.setAttribute("owner", owner);
						dispatcher.forward(request, response);
					} else {
						connect.setSID(connect.getdbSession(request));
						connect.setUser(account.strUserName);
						connect.setPassword(account.strPass);
						Connection conn = connect.simple_connect();
						String owner = (String) request.getParameter("owner");

						DMLTableInterface db = new SaleReportTable(conn, owner);
						// selectsc
						LinkedList<Sales> lst = db.select();
						SQLModel db1 = new SQLModel(conn);
						LinkedList<String> lst1 = db1.getAllUsers();
						conn.close();
						RequestDispatcher dispatcher = getServletContext()
								.getRequestDispatcher(
										"/JSP/adminView/salerTables.jsp");
						request.setAttribute("lstSale", lst);
						request.setAttribute("lstOwner", lst1);
						request.setAttribute("owner", owner);
						dispatcher.forward(request, response);
					}

				} catch (Exception e) {
					System.out.print("Connection unsuccessfly "
							+ e.getMessage());
					// response.sendRedirect("ManageAccount/Login.jsp");
				}
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
